package com.pb.metadata.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pb.infra.oth.entity.common.Page;
import com.pb.infra.oth.entity.common.Result;
import com.pb.infra.oth.util.JwtTokenConfig;
import com.pb.metadata.mapper.DimensionAttrManagementMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 维度编号管理
 */
@Service
public class DimensionAttrManagementService {
    /**
     * DAO映射对象
     */
    @Resource
    private DimensionAttrManagementMapper dimensionAttrManagementMapper;

    /**
     * jwttoken配置类
     */
    @Resource
    private JwtTokenConfig jwtTokenConfig;

    /**
     * 查询分页数据
     * @param map 查询参数
     * @param page 页码
     * @return
     */
    public Result select(Map<String, Object> map, Page page) {
        if (page != null && page.getPageIndex() != null && page.getPageSize() != null) {
            PageHelper.startPage(page.getPageIndex(), page.getPageSize());
        }
        List<Map<String, Object>> list = dimensionAttrManagementMapper.select(map);
        return Result.ok(new PageInfo(list).getTotal(), list);
    }

    @Transactional
    public Result addDimAttrData(Map<String, Object> paramMap) {
        paramMap.put("activeFlag","1");
        paramMap.put("deleteFlag","0");
        dimensionAttrManagementMapper.addDimAttrData(paramMap);
        //查询公共维度类型表，获取注册属性数据，插入corp表中
        List<Map<String, Object>> lists = dimensionAttrManagementMapper.selectDimAttrByDimNo(paramMap);
        for (Map<String, Object> map : lists) {
            map.put("corpId",paramMap.get("corpId"));
            dimensionAttrManagementMapper.insertDimAttrRegData(map);
        }
        return Result.OK_EMPTY;
    }


    public Result updateDimAttrData(Map<String, Object> paramMap) {
        dimensionAttrManagementMapper.updateDimAttrData(paramMap);
        return Result.ok("修改成功");
    }

    public Result getDimAttrRegDataByDimNo(Map<String, Object> paramMap) {
        List<Map<String, Object>> list = dimensionAttrManagementMapper.getDimAttrRegDataByDimNo(paramMap);
        //如果已关联没有选择码值，就取消关联
        for (Map<String, Object> map : list) {
            if(map.get("relaStdCode") == null){
                dimensionAttrManagementMapper.updaterelaCdFlag(map);
            }
        }
        list = dimensionAttrManagementMapper.getDimAttrRegDataByDimNo(paramMap);
        return Result.ok(list);
    }

    public Result addDimAttrRegData(Map<String, Object> paramMap) {
        dimensionAttrManagementMapper.addDimAttrRegData(paramMap);
        return Result.ok("添加成功");
    }

    public Result validUnique(Map<String, Object> paramMap) {
        Map map = dimensionAttrManagementMapper.validUnique(paramMap);
        if (map == null) {
            return Result.ok(true);
        } else {
            return Result.ok(false);
        }
    }

    public Result updateDimAttrRegData(Map<String, Object> paramMap) {
        if(paramMap.get("type") != null && paramMap.get("type").equals("changeRelaStdCode")){
            dimensionAttrManagementMapper.updateDimAttrValue(paramMap);
        }
        dimensionAttrManagementMapper.updateDimAttrRegData(paramMap);
        return Result.OK_EMPTY;
    }

    public Result delAttrRegData(List<Map<String, Object>> attrRegList) {
        for (Map<String, Object> map : attrRegList) {
            dimensionAttrManagementMapper.delAttrRegData(map);
            dimensionAttrManagementMapper.delAttrRegValue(map);
        }
        return Result.OK_EMPTY;
    }

    public Result delDimAttrData(List<Map<String, Object>> dimNoList) {
        for (Map<String, Object> map : dimNoList) {
            dimensionAttrManagementMapper.delDimAttrData(map);
            dimensionAttrManagementMapper.delAttrRegData(map);
            dimensionAttrManagementMapper.delAttrRegValue(map);
        }
        return Result.OK_EMPTY;
    }
}
